<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<jsp:include page="directive.jsp" />
<jsp:include page="header.jsp" />
<jsp:useBean id="cust" class="com.aal.model.CustomerManagement" scope="page"/>

<c:if test="${empty user.username}">
    <jsp:forward page="login.jsp">
        <jsp:param name="from" value="account.jsp"/>
    </jsp:forward>
</c:if>
<c:choose>
<%-- postback --%>
<c:when test="${!empty param.firstname}">
    <jsp:setProperty name="cust" property="username" value="${user.username}"/>
    <jsp:setProperty name="cust" property="customergroupId" value="${user.customer.customerGroupID.customerGroupID}"/><%--normal group --%>
    <jsp:setProperty name="cust" property="firstname" value="${param.firstname}"/>
    <jsp:setProperty name="cust" property="lastname" value="${param.lastname}"/>
    <jsp:setProperty name="cust" property="email" value="${param.email}"/>
    <jsp:setProperty name="cust" property="phone" value="${param.phone}"/>
    <jsp:setProperty name="cust" property="billingaddress" value="${param.billingAddress}"/>
    <c:choose>
    <c:when test="${param.password == param.passwordConfirm}">
        <jsp:setProperty name="cust" property="password" value="${param.password}"/>
        <c:choose>
        <c:when test="${cust.edituser == true}">
            <c:set var="success" value="Your account info has been updated"/>
        </c:when>
        <c:otherwise>
            <c:set var="error" value="Error while trying to connect to database"/>
        </c:otherwise>
        </c:choose>
    </c:when>
    <c:otherwise>
        <c:set var="error" value="Your password confirmation is not correct"/>
    </c:otherwise>
    </c:choose>
</c:when>
<%-- firt time --%>
<c:otherwise>
    <jsp:setProperty name="cust" property="username" value="${user.username}"/>
    <jsp:setProperty name="cust" property="customergroupId" value="${user.customer.customerGroupID.customerGroupID}"/><%--normal group --%>
    <jsp:setProperty name="cust" property="firstname" value="${user.customer.firstname}"/>
    <jsp:setProperty name="cust" property="lastname" value="${user.customer.lastname}"/>
    <jsp:setProperty name="cust" property="email" value="${user.customer.email}"/>
    <jsp:setProperty name="cust" property="phone" value="${user.customer.phone}"/>
    <jsp:setProperty name="cust" property="billingaddress" value="${user.customer.billingAddress}"/>
    <jsp:setProperty name="cust" property="password" value=""/>
</c:otherwise>

</c:choose>

    <div id="inner_wrap">
	<div id="inner_hold">
	<div id="inner">
	<div class="center_hold">
		<c:if test="${empty user.username}">
			<jsp:forward page="login.jsp"/>
		</c:if>
		
        <h1>Hello, ${user.username}!<h1>
		<h2>This is your account information</h2>

        <c:if test="${!empty param.success}">
            <p>${param.success}</p>
        </c:if>

        <c:if test="${!empty param.error}">
            <p style="color:red">${param.error}</p>
        </c:if>
        
		<div id="form_big" class="f11">
			<form action="account.jsp" method="post">

			<div><label for="username">Username</label></div>
			<p><input name="username" type="text" class="sinput" maxlength="25" value="${cust.username}" disabled/></p>
			<p class="f11_blue" style="padding: -4px 0 5px 0">^ You cannot change your username</p>

			<div><label for="email">Email Address</label></div>
			<p><input name="email" type="text" class="sinput" value="${cust.email}" required/></p>
			<p class="f11_blue" style="padding: -4px 0 5px 0">^ Please double check your email</p>

			<div><label for="password">Password</label></div>
			<p><input name="password" type="password" class="sinput" required/></p>
			<p class="f11_blue" style="padding: -4px 0 5px 0">^ Enter your new password here</p>

			<div><label for="passwordConfirm">Confirm Password</label></div>
			<p><input name="passwordConfirm" type="password" class="sinput" required/></p>
            <p class="f11_blue" style="padding: -4px 0 5px 0">^ Enter your new password again</p>

			<div><label for="firstname">First Name</label></div>
			<p><input name="firstname" type="text" class="sinput" value="${cust.firstname}" required/></p>
			
			<div><label for="lastname">Last Name</label></div>
			<p><input name="lastname" type="text" class="sinput" value="${cust.lastname}" required/></p>

			<div><label for="billingAddress">Billing Address</label></div>
            <p><textarea name="billingAddress" rows="4" class="sinput" required>${cust.billingaddress}</textarea></p>

            <div><label for="phone">Phone</label></div>
            <p><input name="phone" type="text" class="sinput" value="${cust.phone}" type="number" required/></p>
            <p class="f11_blue" style="padding: -4px 0 5px 0">^ Please double check your email</p>

			<div class="blue_sep">
				<p><input type="submit" id="submit" class="but_join" value="Save my account info" /></p>
				<p class="empty"></p>
			</div>
			</form>
		</div>
	</div>
	</div>
	</div>
	</div>

<jsp:include page="footer.jsp" />
